package com.zxx.simple;

import com.rabbitmq.client.*;
import com.zxx.utils.ConnectionUtil;

import java.io.IOException;

import static com.zxx.simple.Producer.QUEUE_NAME;

public class Consumer {

    public static void main(String[] args) throws Exception {

        Connection connection = ConnectionUtil.getConnection();

        Channel channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, true, false, false, null);

        DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("路由："+envelope.getRoutingKey());
                System.out.println("交换机："+envelope.getExchange());
                System.out.println("消息id为："+envelope.getDeliveryTag());
                System.out.println("接收到的消息为："+new String(body, "utf-8"));
            }
        };

        channel.basicConsume(QUEUE_NAME,true,defaultConsumer);
    }
}
